/**
 * Created by udi on 2016/11/28.
 */


require(["jv", "vue"], function () {
  var Vue = require("vue");

  Vue.config.ignoredElements = ["kv", "k", "v"];

  if (!location.map.token) {
    jv.setToken();
  }

  var skip = 0, take = 5;

  var OrderStatusEnum = jv.enumer({
    waitPay: "未付款",
    paid: "已付款",
    confirmed: "已接单",
    working: "服务中",
    completed: "服务完成",
    closed: "已关闭",
    cancelled: "已取消"
  });

  var vm = jv.vm = new Vue({
    el: '#app',
    data: {items: [], loading: true},
    computed: {},
    methods: {
      getStatus: function (status) {
        return OrderStatusEnum.getData(status);
      },
      cancel: function (ev) {
        var self = this;
        jv.post("/order/cancel", {orderId: this.id}, function (res) {
          self.status = "cancelled";
        });
      },
      pay: function (ev) {
        if (window.android && window.android.pay) {
          window.android.pay(this.id);
        }
        else {
          alert("找不到支付接口！");
        }
      }
    }
  });


  var bindVue = function (data) {
    skip += data.length;
    data.splice(0, 0, jv.vm.$data.items.length);
    data.splice(1, 0, 0);

    if( data.length <= take){
      jv.vm.$data.loading = false;
    }

    jv.vm.$data.items.splice.apply(jv.vm.$data.items, data);
  }

  jv.scroll2Bottom(function () {
    jv.post("/order/list", {skip: skip, take: take}, bindVue);
  }, true);
});
